<img src='/assets/images/logo.png' alt='logo elabftw' class='col-md-3' />
<div class='separator col-md-4 mx-auto'></div>

{# Allow non existing user to request access to a team #}
{% if App.Session.has('initial_team_selection_required') %}
  <h2>{{ 'Request access to a team'|trans }}</h2>
  <br>
  <form method='post' class='form-group' id='init-team-select' action='app/controllers/LoginController.php' autocomplete='off'>
    <input type='hidden' name='auth_type' value='teaminit'>
    {{ App.Session.get('csrf')|csrf }}
    <div class='form-group mx-auto col-md-4'>
      <label for='init_team_select'>{{ 'Select a team'|trans }}</label>
      <select required name='team_id' class='form-control' id='init_team_select' autocomplete='off'>
        <option selected disabled value=''>{{ 'Select a team'|trans }}</option>
        {% for team in teamsArr %}
          <option value='{{ team.id }}'>{{ team.name }}</option>
        {% endfor %}
      </select>
      <label for='teaminit_firstname_input'>{{ 'Firstname'|trans }}</label>
      <input required class='form-control' name='teaminit_firstname' value='{{ App.Session.get('teaminit_firstname') }}' />

      <label for='teaminit_lastname_input'>{{ 'Lastname'|trans }}</label>
      <input required class='form-control' name='teaminit_lastname' value='{{ App.Session.get('teaminit_lastname') }}' />
      <button class='btn btn-primary mt-2' type='submit' name='submit'>{{ 'Request access'|trans }}</button>
    </div>
  </form>
{# Allow existing user to request access to a team #}
{% elseif App.Session.has('team_request_selection_required') %}
  <h2>{{ 'Request access to a team'|trans }}</h2>
  <br>
  <form method='post' class='form-group' id='team-select' action='app/controllers/LoginController.php' autocomplete='off'>
    <input type='hidden' name='auth_type' value='teamselection'>
    {{ App.Session.get('csrf')|csrf }}
    <div class='form-group mx-auto col-md-4'>
      <label for='existing_user_team_select'>{{ 'Select a team'|trans }}</label>
      <select required name='team_id' class='form-control selectpicker' id='existing_user_team_select' data-show-subtext='true' data-live-search='true'>
        <option selected disabled value=''>{{ 'Select a team'|trans }}</option>
        {% for team in teamsArr %}
          <option value='{{ team.id }}'>{{ team.name }}</option>
        {% endfor %}
      </select>
      <button class='btn btn-primary mt-2' type='submit' name='submit'>{{ 'Request access'|trans }}</button>
    </div>
  </form>
{# Show message when initial team selection is done #}
{% elseif App.Session.has('teaminit_done') %}
  {% if App.Session.get('teaminit_done_need_validation') == '1' %}
    {{ 'Your account has been created and will need to be validated by an admin.'|trans|msg('ok') }}
  {% else %}
    {{ 'Your account has been created and is now active. Try logging in again.'|trans|msg('ok') }}
    <a href='login.php?rm_teaminit=1' class='btn btn-primary'>{{ 'Continue'|trans }}</a>
  {% endif %}

{% elseif App.Session.has('team_selection_required') %}
  <form method='post' action='app/controllers/LoginController.php'>
    <input type='hidden' name='auth_type' value='team'>
    {% if App.Session.get('rememberme') == '1' %}
      <input type='hidden' name='rememberme' value='on'>
    {% endif %}
    {{ App.Session.get('csrf')|csrf }}
    <div class='col-md-4 mx-auto mt-4 text-left form-group'>
      {% if App.devMode -%}
        <!-- [html-validate-disable-block input-missing-label, prefer-native-element: suppress errors from tom-select] -->
      {%- endif %}
      <label for='team_selection_select'>{{ 'Your account is linked to several teams. Select in which team you want to log in'|trans }}</label>
      <select name='selected_team' id='team_selection_select' class='form-control'>
        {% for team in App.Session.get('team_selection') %}
          <option value='{{ team.id }}'>{{ team.name }}</option>
        {% endfor %}
      </select>
    </div>
    <button type='submit' class='btn btn-primary' name='Submit'>{{ 'Login'|trans }}</button>
  </form>
{% elseif App.Request.server.get(App.Config.configArr.extauth_remote_user) %}
  {# user is already authed with external auth, so just present a continue button
    we don't autologin anymore #}
    <form method='post' id='login' action='app/controllers/LoginController.php' autocomplete='off' aria-label='autologin form'>
      {{ App.Session.get('csrf')|csrf }}
      <input type='hidden' name='auth_type' value='external'>
      <h2 class='mt-4'>{{ 'You are already authenticated'|trans }}</h2>
      <br>
      <button type='submit' class='btn btn-primary' name='Submit'>{{ 'Continue'|trans }}</button>
    </form>
  {% else %}

  {% if showLocal or App.Config.configArr.ldap_toggle %}
    {# Login form , the id is for an acceptance test #}
    <form method='post' id='login' action='app/controllers/LoginController.php' autocomplete='off' aria-label='local or ldap login'>
      {{ App.Session.get('csrf')|csrf }}
      <h1 class='my-4 '>{{ 'Login to your account'|trans }}</h1>
      <div class='col-md-4 mx-auto text-left form-group'>
        <label for='email'>{{ App.Config.configArr.ldap_search_attr == 'mail' ? 'Email'|trans : 'Login'|trans }}</label>
        <input class='form-control mb-4' id='email' name='email' type='{{ App.Config.configArr.ldap_search_attr == 'mail' ? 'email' : 'text' }}' value='{{ App.Session.has('email') ? App.Session.get('email') }}' autocomplete='email' required />
        <label for='password'>{{ 'Password'|trans }}</label>
        <div class='input-group'>
          <input class='form-control' id='password' name='password' type='password' autocomplete='current-password' required />
          <div class='input-group-append'>
            <button type='button' class='btn btn-light input-border' data-action='toggle-password' title='{{ 'Show password'|trans }}' aria-label='{{ 'Show password'|trans }}'><i class='fas fa-eye' aria-hidden='true'></i></button>
          </div>
        </div>

        <div class='d-flex align-itemc-center mt-2'>
          {# Remember me checkbox #}
          {% if App.Config.configArr.remember_me_allowed %}
            <div class='form-check'>
              <input type='checkbox' class='form-check-input' {{ App.Config.configArr.remember_me_checked ? 'checked' }} name='rememberme' id='rememberme-1' />
              <label for='rememberme-1' class='form-check-label'>{{ 'Remember me'|trans }}</label>
            </div>
          {% endif %}

          {% if showLocal %}
            <button type='button' class='btn underlined lh-normal border-0 p-0 m-0 ml-auto link-like' style='font-weight:500;' data-action='toggle-modal' data-target='resetModal'>{{ 'Forgot password?'|trans }}</button>
          {% endif %}
        </div>
      </div>

      {% if App.Config.configArr.ldap_toggle %}
      {# Select login type (local or ldap) #}
      <div class='form-check form-check-inline'>
        {% if showLocal %}
          <input class='form-check-input' type='radio' name='auth_type' id='auth_type_local' value='local' {{ App.Config.configArr.ldap_toggle == '0' ? 'checked' }}>
          <label class='form-check-label' for='auth_type_local'>{{ 'Local'|trans }}</label>
        {% endif %}
      </div>
      <div class='form-check form-check-inline'>
        <input class='form-check-input' type='radio' name='auth_type' id='auth_type_ldap' value='ldap' {{ App.Config.configArr.ldap_toggle == '1' ? 'checked' }}>
        <label class='form-check-label' for='auth_type_ldap'>LDAP</label>
      </div>
      {% else %}
        <input type='hidden' name='auth_type' value='local'>
      {% endif %}

      {# Login button #}
      <button type='submit' class='btn btn-primary' name='Submit'>{{ 'Login'|trans }}</button>
    </form>
    {% if App.Config.configArr.local_register %}
    <p class='mt-4'>{{ "Don't have an account? %sRegister%s now!"|trans|format("<a href='register.php'>", "</a>")|raw }}</p>
    {% endif %}

  {% endif %}

  {# SAML LOGIN #}
  {% if App.Config.configArr.saml_toggle and idpsArr|length %}
    <h2>{{ 'Login through your institution'|trans }}</h2>
    <br>
    <form method='post' class='form-group' id='saml-login' action='app/controllers/LoginController.php' autocomplete='off'>
      <input type='hidden' name='auth_type' value='saml'>
      {{ App.Session.get('csrf')|csrf }}
      <div class='form-group col-md-4 mx-auto'>
        {% if App.devMode -%}
          <!-- [html-validate-disable-block input-missing-label, prefer-native-element: suppress errors from tom-select] -->
        {%- endif %}
        <label for='idp_login_select'>{{ 'Select an identity provider'|trans }}</label>
        <select name='idpId' class='form-control' id='idp_login_select'>
          {% for idp in idpsArr %}
            <option value='{{ idp.id }}'>{{ idp.name }}</option>
          {% endfor %}
        </select>
        {# No remember me checkbox for SAML because it's toggled by remember_me_allowed at the instance level #}
        <button class='btn btn-primary mt-2' type='submit' name='submit'>{{ 'Login'|trans }}</button>
      </div>
    </form>
  {% endif %}

  {% if App.Config.configArr.anon_users %}
    <h2>{{ 'Login as anonymous visitor'|trans }}</h2>
    <br>
    <form method='post' class='form-group' id='anon-login' action='app/controllers/LoginController.php' autocomplete='off'>
      <input type='hidden' name='auth_type' value='anon'>
      {{ App.Session.get('csrf')|csrf }}
      <div class='form-group mx-auto col-md-4'>
        <label for='anon_login_select' class='sr-only'>{{ 'Select a team'|trans }}</label>
        <div class='input-group mb-2'>
          <div class='input-group-prepend'>
            <div class='input-group-text'>{{ 'Select a team'|trans }}</div>
          </div>
          <select name='team_id' class='form-control' id='anon_login_select'>
            {% for team in teamsArr %}
              <option value='{{ team.id }}'>{{ team.name }}</option>
            {% endfor %}
          </select>
        </div>
        <button class='btn btn-primary mt-2' type='submit' name='submit'>{{ 'Login'|trans }}</button>
      </div>
    </form>
  {% endif %}
{% endif %}
